<template>
  <div class="searchTop">
    <svg class="icon" aria-hidden="true" @click="$router.go(-1)">
      <use xlink:href="#icon-zuojiantou1"></use>
    </svg>
    <input
      type="text"
      placeholder="陈奕迅"
      v-model.trim="searchKey"
      @keyup.enter="enterKey"
    />
  </div>
  <div class="searchHistory">
    <span class="searchSpan">历史</span>
    <span
      v-for="item in keyWorldList"
      :key="item"
      class="keySpan"
      @click="searchHistory(item)"
    >
      {{ item }}</span
    >
    <svg class="icon" aria-hidden="true" @click="delHistory">
      <use xlink:href="#icon-shanchu"></use>
    </svg>
  </div>
  <!-- 歌曲列表区域 -->
  <!-- 歌曲信息 -->
  <div class="message">
    <div class="message-item" v-for="(item, index) in searchList" :key="item.id">
      <div class="message-left" @click="updateIndex(item)" >
        <span>{{ index + 1 }}</span>
        <div class="works">
          <p>{{ item.name }}</p>
          <div class="limitauthor">
            <span
              class="author"
              v-for="(item1, index) in item.artists"
              :key="index"
              >{{ item1.name }}</span
            >
          </div>
        </div>
      </div>
      <div class="message-right">
        <svg class="icon" aria-hidden="true">
          <use xlink:href="#icon-31liebiao"></use>
        </svg>
        <svg class="icon" aria-hidden="true" v-if="item.mvid">
          <use xlink:href="#icon-shipin"></use>
        </svg>
      </div>
    </div>
  </div>
</template>

<script>
import { getSearchMusic } from "@/api/home.js";
import { mapMutations } from 'vuex';
export default {
  data() {
    return {
      keyWorldList: [],
      searchKey: "",
      searchList: [], //搜索列表
    };
  },
  mounted() {
    this.keyWorldList = JSON.parse(localStorage.getItem("keyWorldList"))
      ? JSON.parse(localStorage.getItem("keyWorldList"))
      : [];
  },
  methods: {
    async enterKey() {
      if (this.searchKey == "") {
        return;
      }
      this.keyWorldList.unshift(this.searchKey);
      let mySet = new Set(this.keyWorldList);
      this.keyWorldList = [...mySet];
      // 固定长度
      if (this.keyWorldList.length > 10) {
        this.keyWorldList.splice(this.keyWorldList.length - 1, 1);
      }
      localStorage.setItem("keyWorldList", JSON.stringify(this.keyWorldList));
      //调用根据关键字获取歌曲信息数据函数
      let { data: res } = await getSearchMusic(this.searchKey);
      console.log(res);
      this.searchList = res.result.songs;

      this.searchKey = "";
    },
    delHistory() {
      localStorage.removeItem("keyWorldList");
      this.keyWorldList = [];
    },
    async searchHistory(item) {
      let { data: result } = await getSearchMusic(item);
      // console.log(result);
      this.searchList = result.result.songs;
    },
    // 将数据添加到播放列表
    updateIndex(item){
      item.al=item.album
      item.al.picUrl=item.album.artist.img1v1Url
      item.ar=item.artists
        this.pushPlayList(item)
        this.updateplayListIndex(this.$store.state.playList.length-1)
    },
    ...mapMutations(['pushPlayList','updateplayListIndex'])
  },
};
</script>

<style lang="less" scoped>
.searchTop {
  width: 100%;
  height: 1rem;
  padding: 0 0.2rem;
  display: flex;
  align-items: center;
  input {
    margin-left: 0.2rem;
    border: none;
    border-bottom: 0.02rem solid #999;
    width: 90%;
    padding: 0.1rem;
  }
}
.searchHistory {
  width: 100%;
  padding: 0.2rem;
  position: relative;
  .searchSpan {
    font-weight: 700;
  }
  .keySpan {
    padding: 0.1rem 0.2rem;
    background-color: rgb(185, 169, 169);
    border-radius: 0.4rem;
    margin: 0.1rem 0.2rem;
    display: inline-block;
  }
  .icon {
    width: 0.3rem;
    height: 0.3rem;
    position: absolute;
    right: 0.2rem;
  }
}
 .message {
  width: 100%;
  padding: .2rem;
    .message-item {
      height: 1rem;
      width: 100%;
      // border: 1px solid;
      display: flex;
      .message-left {
        width: 80%;
        display: flex;
        align-items: center;
        .works {
          margin-left: 0.2rem;
          p {
            font-size: 0.32rem;
            font-weight: 600;
            width: 4rem;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
          }
          .limitauthor {
            width: 2rem;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;

            .author {
              font-size: 0.28rem;
              color: gray;
              margin-right: 0.1rem;
            }
          }
        }
      }
      .message-right {
        width: 20%;
        display: flex;
        align-items: center;
        flex-direction: row-reverse;
        justify-content: space-between;
        padding-left: .3rem;
        .icon {
          fill: gray;
          width: 0.4rem;
          height: 0.4rem;
        }
      }
    }
  }
</style>